package com.rmfyzxfw.caseprocess.util;


import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.MessageFormat;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @description:
 * @Author: GYW  742926306@qq.com
 * @Date: 2023/7/3 14:44
 */
@Slf4j
public class DateUtil {
    private static final String yyyyMMddHHmmss = "yyyy-MM-dd HH:mm:ss";
    private static final String yyyyMMddHH = "yyyy-MM-dd HH";
    private static final String yyyyMMdd = "yyyy-MM-dd";
    private static final DateTimeFormatter yyyyMMddHHmmssFormatter = DateTimeFormatter.ofPattern(yyyyMMddHHmmss);
    private static final DateTimeFormatter yyyyMMddHHFormatter = DateTimeFormatter.ofPattern(yyyyMMddHH);
    private static final DateTimeFormatter yyyyMMddFormatter = DateTimeFormatter.ofPattern(yyyyMMdd);

    /**
     * @param dateStr
     * @description: 将字符串转为LocalDateTime
     * @return: java.time.LocalDateTime
     */
    public static LocalDateTime str2localDateTime(String dateStr) {
        if(StringUtils.isBlank(dateStr)){
            return LocalDateTime.now();
        }
        return LocalDateTime.parse(dateStr, yyyyMMddHHmmssFormatter);
    }

    /**
     * @param localDateTime
     * @description: 将LocalDateTime转为字符串
     * @return: java.lang.String
     */
    public static String localDateTime2str(LocalDateTime localDateTime) {
        return localDateTime.format(yyyyMMddHHmmssFormatter);
    }

    /**
     * @param localDateTime
     * @return
     * @description: 将LocalDateTime转为年月日
     */
    public static String localDateTime2YearMonthDay(LocalDateTime localDateTime) {
        return MessageFormat.format("{0}年{1}月{2}日", String.valueOf(localDateTime.getYear()), String.valueOf(localDateTime.getMonthValue()),
                String.valueOf(localDateTime.getDayOfMonth()));
    }

    public static String getYyyyMMddHH(LocalDateTime localDateTime) {
        return localDateTime.format(yyyyMMddHHFormatter);
    }

    public static String getYyyyMMdd(LocalDateTime localDateTime) {
        return localDateTime.format(yyyyMMddFormatter);
    }

    /**
     * @param dateStr
     * @description: 将字符串转为LocalDate
     * @return: java.time.LocalDateTime
     */
    public static LocalDate str2localDate(String dateStr) {
        return LocalDate.parse(dateStr, yyyyMMddFormatter);
    }

    /**
     * @param localDate
     * @description: 将LocalDateTime转为字符串
     * @return: java.lang.String
     */
    public static String localDate2str(LocalDate localDate) {
        if(localDate == null){
            return LocalDate.now().format(yyyyMMddFormatter);
        }
        return localDate.format(yyyyMMddFormatter);
    }
}